package cn.edu.xmu.restfuldemo.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.models.auth.In;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 22920192204242
 * @date 2021/10/14 16:52
 */
@Data
@ApiModel(description = "订单视图对象")
public class OrdersVo {
    private static Logger logger = LoggerFactory.getLogger(OrdersVo.class);

    @NotNull(message="订单明细不能为空")
    @ApiModelProperty(value = "订单明细")
    private List<OrderItemVo> orderItems;

    @NotBlank(message="收货人不能为空")
    @ApiModelProperty(value = "收货人")
    private String consignee;

    @ApiModelProperty(value = "地区ID")
    private Integer regionId;

    @ApiModelProperty(value = "地区")
    private String address;

    @ApiModelProperty(value = "预售活动Id")
    private Integer presaleId;

    @ApiModelProperty(value = "联系电话")
    private String mobile;

    @ApiModelProperty(value = "团购活动ID")
    private Integer grouponId;

    @ApiModelProperty(value = "优惠券ID")
    private Integer couponId;

    @ApiModelProperty(value = "附言")
    private String message;
    /**
     * 由Vo对象创建Orders对象
     * @return Orders对象
     */
    public Orders createOrders(){
        Orders orders = new Orders();
        orders.setAddress(this.address);
        orders.setCouponId(this.couponId);
        orders.setMessage(this.message);
        orders.setGrouponId(this.grouponId);
        orders.setMobile(this.mobile);
        orders.setPresaleId(this.presaleId);
        orders.setConsignee(this.consignee);
        orders.setRegionId(this.regionId);

        if (null != this.orderItems) { //Product == OrderItem    productvo = orderitemvo
            logger.info("createOrders: this.orderItems = "+this.orderItems);
            List<OrderItem> newOrderItemList = new ArrayList<>(this.orderItems.size());

            for (OrderItemVo orderItemVo : this.orderItems) {
                OrderItem orderItem = orderItemVo.createOrderItem();
                newOrderItemList.add(orderItem);
            }
            orders.setOrderItemList(newOrderItemList);

        }

        return orders;
    }
}
